#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int NN=2e5+4;
vector<int>state[NN],t;
map<vector<int>,int>id;
int n,k,idx,cnt[NN],a[NN];
ll f[NN];
void get_id(int u,int now,int all)
{
	if(u==k+1)
	{
		state[++idx]=t;
		id[t]=idx;
		cnt[idx]=all;
		return;
	}
	for(int i=now;~i;i--)
	{
		t.push_back(i);
		get_id(u+1,i,all+i);
		t.pop_back();
	}
}
void dfs(int u,int now,ll res,int w)
{
	if(k-u<now)
		return;
	if(!now)
	{
		int x=id[t];
		f[x]=min(f[x],res);
		return;
	}
	if(!u||t[u]<t[u-1])
	{
		if(!t[u])
			res-=w;
		t[u]++;
		if(t[u]==n/k)
			res+=w;
		dfs(u+1,now-1,res,w);
		if(t[u]==n/k)
			res-=w;
		t[u]--;
		if(!t[u])
			res+=w;
	}
	dfs(u+1,now,res,w);
}
int main()
{
	freopen("diyiti.in","r",stdin);
	freopen("diyiti.out","w",stdout);
	scanf("%d%d",&n,&k);
	get_id(1,n/k,0);
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	sort(a+1,a+1+n);
	memset(f,0x3f,sizeof(f));
	f[idx]=0;
	for(int i=1,j;i<=n;i=j+1)
	{
		j=i;
		while(j<n&&a[j+1]==a[i])
			j++;
		for(int k=1;k<=idx;k++)
			if(cnt[k]==i-1)
			{
				t=state[k];
				dfs(0,j-i+1,f[k],a[i]);
			}
	}
	if(f[1]>1e18)
		f[1]=-1;
	printf("%lld",f[1]);
	return 0;
}
